package com.beiding.dbrequest.common;

import io.reactivex.ObservableEmitter;
import org.reactivestreams.Subscriber;

import java.io.Serializable;

/**
 * 封装异步处理结果
 */
public class ObservableResponseCallback implements ResponseCallback {
    private ObservableEmitter<? super Serializable> subscriber;

    public ObservableResponseCallback(ObservableEmitter<? super Serializable> subscriber) {
        this.subscriber = subscriber;
    }


    @Override
    public void onSuccess(Message message) {

        //2.x不支持发送null对象
        if (message.getBody() != null) {
            subscriber.onNext(message.getBody());
        }
        subscriber.onComplete();
    }

    @Override
    public void onFailed(Message message) {
        Integer status = message.getStatus();
        String msg = "status:" + status + "  msg:" + message.getMsg();
        subscriber.onError(new ResponseException(msg, message));
    }

}
